/*
 * KKFontCache.h
 *
 *  Created on: 2013-9-15
 *      Author: wanghua
 */

#ifndef KKFONTCACHE_H_
#define KKFONTCACHE_H_

#include "kaka_common.h"

#include "XmlUtils.h"

USING_NS_CC;

using namespace std;

class KKStringLine: public CCObject {
public:
	int top;
	int width;
	wstring text;
};

class KKBitmapFontItem: public CCObject {
public:
	wchar_t id;
	CCRect rect;
};

typedef KKDictionary<int, KKBitmapFontItem*> KKBitmapFontItemDict;

class KKBitmapFont: public CCObject {

private:
	KKBitmapFontItemDict m_pBitmapFontArray;

	;CC_SYNTHESIZE_READONLY(string,m_sAtlasName,AtlasName)
	;

public:
	KKBitmapFont();
	virtual ~KKBitmapFont();

	static KKBitmapFont * configurationWithFNTFile(const char *FNTfile);

	/** initializes a BitmapFontConfiguration with a FNT file */
	bool initWithFNTfile(const char *FNTfile);

	KKBitmapFontItem* getItemWithId(wchar_t id) {
		return m_pBitmapFontArray.objectForKey((int) id);
	}
	;

	int getItemCount() {
		return int(m_pBitmapFontArray.count());
	}
	;

};

typedef KKDictionary<string, KKBitmapFont*> KKBitmapFontCache;

class KKFontCache: public cocos2d::CCObject {
private:
	static KKFontCache* _sharedFontCache;

	KKBitmapFontCache bitmapFontCache;
public:
	KKFontCache();
	virtual ~KKFontCache();

	static KKFontCache* sharedFontCache() {
		if (!_sharedFontCache) {
			_sharedFontCache = new KKFontCache();
			_sharedFontCache->autorelease();
			_sharedFontCache->retain();
		}

		return _sharedFontCache;
	}

	KKBitmapFont* getBitmapFont(const char* path) {

		KKBitmapFont* font = bitmapFontCache.objectForKey(path);

		if (!font) {
			font = KKBitmapFont::configurationWithFNTFile(path);
			if (font) {
				bitmapFontCache.setObject(font, path);
			}
		}

		return font;
	}
};

#endif /* KKFONTCACHE_H_ */
